package frame;

import mapper.UserMapper;
import memento.CareTaker;
import memento.UserInfoOriginator;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import utils.MybatisUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserFrame extends MainFrame {
    private JPanel contentPane;

    private JTextField oldpwd;

    private JTextField userid;

    private JTextField newpwd;
    private JTextField secpwd;


    private static String userID = LoginFrame.id;
    // 获取SqlSession对象
    private SqlSession sqlSession = MybatisUtil.getSession();
    // 创建userDao接口，然后通过反射来获取代理对象
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    private boolean check(String str) {
        return str == null || str.equals("");
    }

    // 创建一个备忘录管理员
    private CareTaker careTaker = new CareTaker();

    private UserInfoOriginator uiog = new UserInfoOriginator();

    UserFrame(){
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setBounds(141, 36, 637, 476);
        getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("用户信息");
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        lblNewLabel.setBounds(141, 46, 238, 40);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("用户账号：");
        lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
        lblNewLabel_1.setBounds(73, 118, 125, 32);
        panel.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("旧密码：");
        lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
        lblNewLabel_2.setBounds(73, 160, 125, 32);
        panel.add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("新密码：");
        lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_3.setFont(new Font("微软雅黑", Font.BOLD, 14));
        lblNewLabel_3.setBounds(73, 202, 125, 32);
        panel.add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("再次输入密码：");
        lblNewLabel_4.setFont(new Font("微软雅黑", Font.BOLD, 14));
        lblNewLabel_4.setBounds(73, 244, 125, 32);
        panel.add(lblNewLabel_4);

        userid = new JTextField();
        userid.setBounds(191, 118, 160, 32);
        panel.add(userid);
        userid.setColumns(10);

        oldpwd = new JTextField();
        oldpwd.setColumns(10);
        oldpwd.setBounds(191, 160, 160, 32);
        panel.add(oldpwd);

        newpwd = new JTextField();
        newpwd.setColumns(10);
        newpwd.setBounds(191, 202, 160, 32);
        panel.add(newpwd);

        secpwd = new JTextField();
        secpwd.setColumns(10);
        secpwd.setBounds(191, 244, 160, 32);
        panel.add(secpwd);

        userid.setText(userID);

        JButton btnNewButton = new JButton("修改");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 先确定旧密码是否输入正确，若输入正确且新密码输入不为空，则直接修改
                String oldp = oldpwd.getText(), newp = newpwd.getText();
                if (check(oldp) || check(newp)) {
                    JOptionPane.showMessageDialog(null, "输入不能为空！");
                }
                User nowU = userMapper.findUserById(userID);

                if (!oldp.equals(nowU.getUserpwd())) {
                    JOptionPane.showMessageDialog(null, "旧密码输入错误！");
                }
                User tmp = new User(userID, newp);
                int cnt = userMapper.updateUserById(tmp);
                if (cnt > 0) {
                    sqlSession.commit();
                    JOptionPane.showMessageDialog(null, "修改成功！");
                    // 备忘录保存状态值
                    uiog.setUserpwd(newp);
                    careTaker.add(uiog.saveToMemento());
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败！");
                    setVisible(false);
                    new UserFrame().setVisible(true);
                }
            }
        });
        btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton.setBounds(75, 307, 100, 40);
        panel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("撤销");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //System.err.println("用户点击了撤销");
                // 保留的状态大于1才可进行撤销操作
                int n = careTaker.getMementoList().size();
                //System.err.println(n);
                if (n > 1) {
                    System.err.println("执行撤销操作");
                    // 撤销返回上一级操作
                    uiog.restoreFromMemento(careTaker.getMementoList().get(n - 2));
                    // 并且删除掉最后一个
                    careTaker.getMementoList().remove(n - 1);
                    // 更新用户信息
                    User tmp = new User(userID, uiog.getUserpwd());
                    int cnt = userMapper.updateUserById(tmp);
                    if (cnt > 0) {
                        sqlSession.commit();
                        updateData(userID, uiog.getUserpwd());
                        newpwd.setText("");
                        secpwd.setText("");
                        JOptionPane.showMessageDialog(null, "撤销成功！");
                        // 修改表单状态值
                    } else {
                        JOptionPane.showMessageDialog(null, "撤销失败！");
                    }
                }
            }
        });
        btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton_1.setBounds(191, 307, 100, 40);
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("重置");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                oldpwd.setText("");
                newpwd.setText("");
                secpwd.setText("");
            }
        });
        btnNewButton_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
        btnNewButton_2.setBounds(307, 307, 100, 40);
        panel.add(btnNewButton_2);


    }

    private void updateData(String uid, String upwd) {
        userid.setText(uid);
        oldpwd.setText(upwd);
    }
}
